//
//  AppDelegate.m
//  iPhoneSafety
//
//  Created by dl on 2018/12/29.
//  Copyright © 2018 com.li.ding. All rights reserved.
//

#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import <UserNotifications/UserNotifications.h>
#import "ISSafetyLocalNotificationManager.h"

@interface AppDelegate () <UNUserNotificationCenterDelegate>
@property (nonatomic, assign) UIBackgroundTaskIdentifier taskID;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    if (@available(iOS 10.0, *)) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        }];
    }
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    self.taskID = UIBackgroundTaskInvalid;
    
    [self startBackgroundTask];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self stopBackgroundTask];
}

- (void) startBackgroundTask {
    NSError *audioSessionError = nil;
    BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                                    withOptions:AVAudioSessionCategoryOptionMixWithOthers
                                                          error:&(audioSessionError)];
    NSLog(@"set audio session flag: %@", @(success));
    
    self.taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:self.taskID];
        self.taskID = UIBackgroundTaskInvalid;
    }];
}

- (void) stopBackgroundTask {
    [[UIApplication sharedApplication] endBackgroundTask:self.taskID];
    self.taskID = UIBackgroundTaskInvalid;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"notification.userInfo=%@", notification.userInfo);
}

#pragma mark - UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0)) {
//    completionHandler(UNNotificationPresentationOptionAlert);
}

@end
